import React, { Component } from 'react';
import { ImageBackground, FlatList, View, Image, RefreshControl, Text, TouchableOpacity, TextInput, StyleSheet } from 'react-native';
import { unitWidth, width } from '../constants/AdapterUtil';
import { MyHeader, MyRightImageHeader } from '../components/MyHeader';
import Colors from '../constants/Colors';
import MyListView from '../components/MyListView';
import ScrollableTabView, { ScrollableTabBar, DefaultTabBar } from 'react-native-scrollable-tab-view';
import NavigationService from '../constants/NavigationService';
import { SubmitButton7, SubmitButton1 } from '../components/Button';
import { CommonModal3 } from '../components/Modal';
import { api_order_workorder_workorderList, api_order_workorder_ITQ } from '../constants/HttpServices';
import { postFetch } from '../constants/HttpExtension';
import { show } from '../constants/Toast';

export default class ChooseMyDemandScreen extends Component {

    constructor(props) {
        super(props)
        this.merchid = this.props.navigation.getParam('merchid', -1)
        this.state = {
            list: [],

            currentChoose: -1,
        }
    }

    componentDidMount() {
        this.getData(1);
    }

    render() {

        return (
            <View style={{ flex: 1, backgroundColor: Colors.colorF5 }}>
                <MyHeader title='我的需求' leftImg={require('../res/ic_arrow_left.png')} leftClick={() => { this.props.navigation.pop() }} />
                <MyListView
                    ref='MyListView'
                    list={this.state.list}
                    renderItem={this.getItem.bind(this)}
                    getList={this.getData.bind(this)}
                />
                <SubmitButton1 style={{ alignSelf: 'center', marginVertical: 20 * unitWidth, }} text='确定' onPress={() => {
                    this.submit()
                }} />
                <CommonModal3 ref='CommonModal3' title={'恭喜你邀请成功\n请耐心等待服务商报价哦'} btnText='确定'
                    onPress={() => {
                        this.refs.CommonModal3.hideModal();
                        this.props.navigation.pop();
                    }}
                    onClosePress={() => {
                        this.props.navigation.pop();
                    }} />
            </View>
        );
    }

    getItem({ item, index }) {
        return (
            <TouchableOpacity style={{
                width: width,
                height: 161 * unitWidth,
                backgroundColor: Colors.white, justifyContent: 'center'
            }} activeOpacity={0.7}
                onPress={() => {
                    this.setState({
                        currentChoose: index
                    })
                }}>
                <View style={{
                    paddingHorizontal: 20 * unitWidth,
                    flexDirection: 'row', alignItems: 'center'
                }}>
                    <Image style={{ width: 115 * unitWidth, height: 90 * unitWidth }} source={{ uri: item.picture }} resizeMode='stretch' />
                    <View style={{ alignItems: 'flex-start', marginLeft: 7 * unitWidth }}>
                        <Text style={{ color: Colors.black, fontSize: 15 }}>{item.title}</Text>
                        <Text style={{ color: Colors.color99, fontSize: 13, marginTop: 7 * unitWidth }}>加工数目：{item.total_count}</Text>
                        <View style={{ flexDirection: 'row', alignItems: 'center', marginTop: 7 * unitWidth }}>
                            <Image style={{ width: 13 * unitWidth, height: 16 * unitWidth }} source={require('../res/syss_dddw.png')} resizeMode='stretch' />
                            <Text style={{ color: Colors.color99, fontSize: 13, marginLeft: 5 * unitWidth }}>地区：{item.province + '   ' + item.city}</Text>
                        </View>
                        <View style={{ flexDirection: 'row', alignItems: 'center', marginTop: 7 * unitWidth }}>
                            <Image style={{ width: 14 * unitWidth, height: 14 * unitWidth }} source={require('../res/fws_clsj.png')} resizeMode='stretch' />
                            <Text style={{ color: Colors.color99, fontSize: 13, marginLeft: 5 * unitWidth }}>截止报价日期：</Text>
                            <Text style={{ color: Colors.colorOrange, fontSize: 13, marginLeft: 5 * unitWidth }}>{item.end_date}</Text>
                        </View>
                    </View>
                    <TouchableOpacity style={{ alignSelf: 'flex-start', alignItems: 'center', position: 'absolute', right: 15 * unitWidth }}
                        onPress={() => {
                            this.setState({
                                currentChoose: index
                            })
                        }}>
                        <Image style={{ width: 16 * unitWidth, height: 16 * unitWidth }} source={this.state.currentChoose == index ? require('../res/me_txgx2.png') : require('../res/me_txgx22.png')} resizeMode='stretch' />
                    </TouchableOpacity>
                </View>
                <View style={{ flexDirection: 'row', alignItems: 'center', marginTop: 15 * unitWidth, justifyContent: 'space-between', marginHorizontal: 15 * unitWidth }}>
                    <Text style={{ color: Colors.color99, fontSize: 13, marginLeft: 5 * unitWidth }}>
                        已报价服务商
                        <Text style={{ color: Colors.colorOrange }}>   {item.quoted_price_count}家</Text>
                    </Text>
                    <Text style={{ color: Colors.color99, fontSize: 13, marginLeft: 5 * unitWidth }}>{item.createtime}发布</Text>
                </View>
                {index == this.state.list.length - 1 ?
                    null : <View style={{ backgroundColor: Colors.colorE8, width: 338 * unitWidth, alignSelf: 'center', height: 0.5, marginTop: 10 * unitWidth, position: 'absolute', bottom: 0 }} />
                }
            </TouchableOpacity>
        )
    }

    //获取工单信息
    getData(page) {
        let params = {
            page: page,
            my_needs: '1',
            status: '2'
        }
        postFetch(api_order_workorder_workorderList, params, {
            success: (response) => {
                if (page == 1) {
                    this.setState({
                        list: response.result.list,
                    }, () => {
                        this.refs.MyListView.onHasMore();
                    })
                } else {
                    if (response.result.list && response.result.list.length > 0) {
                        this.setState({
                            list: this.state.list.concat(response.result.list),
                        }, () => {
                            this.refs.MyListView.onHasMore();
                        })
                    } else {
                        this.refs.MyListView.onNoMore();
                    }
                }
            },
            fail: (response) => {
                show(response.result.message);
            }
        })
    }

    //邀请报价
    submit() {
        if (this.state.currentChoose == -1) {
            show('请选择邀请对象');
            return
        }
        let params = {
            merchid: this.merchid,
            work_order_id: this.state.list[this.state.currentChoose].id
        }
        postFetch(api_order_workorder_ITQ, params, {
            success: (response) => {
                this.refs.CommonModal3.showModal();
            },
            fail: (response) => {
                show(response.result.message);
            }
        })
    }
}